package com.gz.furnitures.web;


import com.google.gson.Gson;
import com.gz.furnitures.entity.Member;
import com.gz.furnitures.service.impl.MemberServiceImpl;
import com.gz.furnitures.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

@WebServlet(urlPatterns = "/memberServlet")
public class MemberServlet extends BasicServlet {
    MemberServiceImpl memberService = new MemberServiceImpl();

    protected void isExistUserName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        boolean isExistUsername = memberService.isExistsUsername(username);

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("isExist", isExistUsername);

        String strMap = new Gson().toJson(map);
        response.getWriter().write(strMap);
    }

    protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //销毁当前的session
        request.getSession().invalidate();
        //重定向到index.jsp
        response.sendRedirect(request.getContextPath());
    }

    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Member member = memberService.login(new Member(null, username, password, null));
        if (member != null) {
//            System.out.println("登陆成功");
            request.getSession().setAttribute("member", member);
            request.getRequestDispatcher("/views/member/login_ok.jsp").forward(request, response);
        } else {
            //验证失败将用户名和错误信息返回
            request.setAttribute("msg", "用户名或者密码错误");
            request.setAttribute("username", username);
            //返回登录页面
            request.getRequestDispatcher("/views/member/login.jsp").forward(request, response);
//            System.out.println("登录失败");
        }
    }

    protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String code = request.getParameter("code");

        //从session中获取验证码
        String token = (String) request.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //立即删除session验证码，防止该验证码被重复使用
        request.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        if (token != null && token.equalsIgnoreCase(code)) {
            //判断用户名是否可用
            if (!memberService.isExistsUsername(username)) {
//            System.out.println("用户名 "+username+" 不存在，可注册");
                Member member = new Member(null, username, password, email);
                if (memberService.registerMember(member)) {
                    request.getRequestDispatcher("/views/member/register_ok.jsp").forward(request, response);
                } else {
//                System.out.println("注册error");
                    request.getRequestDispatcher("/views/member/register_fail.jsp").forward(request, response);
                }
            } else {
                request.getRequestDispatcher("/views/member/register_fail.jsp").forward(request, response);
            }
        }else{ //验证码不正确
            request.setAttribute("msg2","验证码不正确");
            Member member = DataUtils.copyParamToBean(request.getParameterMap(), new Member());
            request.setAttribute("member", member);
            request.getRequestDispatcher("/views/member/login.jsp").forward(request, response);
        }

    }
}
